$(function (){
    var uploadImage = $(".uploadImage");
    let app = new Vue({
        el: "#app",
        data: {
            dataList: [],//当前页要展示的列表数据
            formData: {},//表单数据
            dialogFormVisible4Edit:false,//编辑表单是否可见
            sysTitle: "固定资源管理系统",
            userID: "", //用户ID
            userName: "", //用户姓名
            userImageSrc: "", //用户头像
            userClass: "",  //用户类型
            userDepartment: "", //用户类型
            userType: "",
            uploadImageSrc: "",
            backImageSrc: "images/common/back.jpg",
            subList1: [
                {href:"properties.html", li: " 资源管理"},
                {href:"departments.html", li: " 部门管理"},
                {href:"employees.html", li: " 员工管理"}
            ],
            subList2: [
                {href:"infos.html", li: " 个人信息"},
                {href:"passwords.html", li: " 密码修改"},
                {href:"util/login.html", li: " 注销"}
            ],
            rules: {//校验规则
                name: [{ required: true, message: '姓名为必填项', trigger: 'blur' }],
                tell: [{ required: true, message: '电话为必填项', trigger: 'blur' }],
                image: [{ required: true, message: '更换头像为必填项', trigger: 'blur' }],
            },
        },
        created(){
            this.getIdAndName();
        },
        methods: {
            // 信息初始化
            getIdAndName(){
                if(sessionStorage.getItem("userID") === null)
                {
                    location.href = "util/login.html";
                }else {
                    //记录用户信息
                    this.userID = sessionStorage.getItem("userID").toString();
                    this.userName = sessionStorage.getItem("userName").toString();
                    this.userImageSrc = "../" + sessionStorage.getItem("userImageSrc").toString();
                    this.userType = sessionStorage.getItem("userType").toString(); // 0 管理员 1 用户
                    this.userClass = sessionStorage.getItem("userClass").toString();
                    this.userDepartment = sessionStorage.getItem("userDepartment").toString();
                    //初始化信息
                    this.getUserInfo();
                }
            },
            //显示用户信息
            getUserInfo(){
                if(this.userType === "0")
                {
                    axios.get("http://localhost:16/mangers/"+this.userID).then((res)=>{
                        this.formData = res.data.data;
                    });
                }else {
                    axios.get("http://localhost:16/employees/"+this.userID).then((res)=>{
                        this.formData = res.data.data;
                    });
                }
            },

            handleQuery() {
                this.getUserInfo();
                this.$message.success("已成功刷新！");
            },
            cancel() {
                this.dialogFormVisible4Edit = false;
                this.$message.info("操作已取消");
            },
            handleUpdate(){
                if(this.formData.tell.length !== 11)
                {
                    this.$message.error("手机号码不正确！");
                    return;
                }
                this.getFileImage();
                if(this.userType === "0")
                {
                    axios.put("http://localhost:16/mangers",this.formData).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.msg);
                            this.dialogFormVisible4Edit = false;
                            this.userName = this.formData.name;
                            sessionStorage.setItem("userImageSrc", "images/upload/" +  this.formData.image );
                        }else this.$message.error(res.data.msg);
                    }).finally(()=>{
                        this.getUserInfo();
                    });
                }else {
                    axios.put("http://localhost:16/employees",this.formData).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.msg);
                            this.dialogFormVisible4Edit = false;
                            this.userName = this.formData.name;
                            sessionStorage.setItem("userImageSrc", "images/upload/" +  this.formData.image );
                        }else this.$message.error(res.data.msg);
                    }).finally(()=>{
                        this.getUserInfo();
                    });
                }
            },
            handleEdit() {
                this.dialogFormVisible4Edit = true;
            },
            getFileImage() {
                if(this.uploadImageSrc !== "")
                {
                    let fileNext = this.uploadImageSrc.split(".");
                    if(fileNext[fileNext.length-1].toLowerCase() === "gif" || fileNext[fileNext.length-1].toLowerCase() === "jpg" || fileNext[fileNext.length-1].toLowerCase() === "png")
                    {
                        let arr = this.uploadImageSrc.split("\\");
                        this.formData.image = arr[arr.length-1];

                        //上传文件

                        //赋值头像
                        this.userImageSrc = "../images/upload/" +  this.formData.image;
                        this.$message.success("上传成功！");
                    }else {
                        this.$message.error("上传头像格式不正确！");
                        this.uploadImageSrc = "";
                        return;
                    }
                }
            }
        }
    });

    /* lis.addClass("current");*/
    var left_nav = $(".left-nav");
    var left_lis = left_nav.children("li");
    var left_sub_nav = left_lis[1].children[1];
    $(left_sub_nav).children("li").eq(0).children("a").addClass("current");

});